package com.nisum.mocksimulator.notifier;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class DirectoryNotifier {
	@Value(".")
	private String path;
	@Value("0")
	private int mask;
	@Value("false")
	private boolean subtree;

	public DirectoryNotifier() {
		this.mask = JNotify.FILE_CREATED  | 
	           JNotify.FILE_DELETED  | 
	           JNotify.FILE_MODIFIED | 
	           JNotify.FILE_RENAMED;
	}
	
	public void configureDirectoryNotifier(String path, boolean subtree){
		this.path = path;
		this.subtree = subtree;
	}
	
	public int start(JNotifyListener listener) {
		int watchID = -1;
		try {
			watchID = JNotify.addWatch(path, mask, subtree, listener);
			System.out.println("watchID is: "+watchID);
		} catch (JNotifyException e) {
			System.out.println("Error starting: "+e.getMessage());
			e.printStackTrace();
		}
		return watchID;
	}
	
	public boolean stop(int watchID) {
		boolean result = false;
		try {
			result = JNotify.removeWatch(watchID);
		} catch (JNotifyException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
}